package com.dushiguang.findhoney;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;


import com.dushiguang.findhoney.View.ConditionFilterView.ConditionFilterView;
import com.dushiguang.findhoney.View.ConditionFilterView.ConditionFilterView.ConditionItem;

import java.util.ArrayList;


/**
 * Created by ex-dushiguang201 on 2015-11-05.
 */
public class MainTestActivity extends Activity implements View.OnClickListener{
    private ConditionFilterView mConditionFilterView;
    private WebView mWebview;
    private Button mBtnGetWebData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mConditionFilterView = (ConditionFilterView) findViewById(R.id.condition_view);
        mBtnGetWebData = (Button) findViewById(R.id.btn_getwebdata);
        mBtnGetWebData.setOnClickListener(this);

        initWebView();
        getConditionData();
        initCondition_view();
    }

    /**
     * 从网络或者,是本地文件,数据库中得到筛选条件的值.
     */
    private void getConditionData() {

    }

    private void initCondition_view() {
        ArrayList<ConditionItem> conditons = new ArrayList<ConditionItem>();
        mConditionFilterView.addConditionCategory("地铁", conditons, 0, "", false);

        mConditionFilterView.addConditionCategory("地铁", conditons, 0, "", false);
    }

    private void initWebView() {
        mWebview = (WebView) findViewById(R.id.mWebview);
        //把本类的一个实例添加到js的全局对象window中，
        //这样就可以使用window.injs来调用它的方法
        mWebview.addJavascriptInterface(new InJavaScript(), "injs");

        WebSettings webSettings = mWebview.getSettings();
        //设置支持JavaScript脚本
        webSettings.setJavaScriptEnabled(true);
        //设置可以访问文件
        webSettings.setAllowFileAccess(true);
        //设置可以缩放
        webSettings.setBuiltInZoomControls(true);

        webSettings.setDatabaseEnabled(true);
        String path = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
        webSettings.setDatabasePath(path);

        //使用localStorage则必须打开
        webSettings.setDomStorageEnabled(true);

        webSettings.setGeolocationEnabled(true);

        //设置WebViewClient
        mWebview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }
        });

    }

    /**
     * 获取WebView数据
     */
    private void getWebViewData() {
        mWebview.loadUrl("http://xf.m.app.qa.anhouse.com.cn/sh/main/detail.id.80448.html?from=hft&hf_sharefrom=ODTSEGyTNWzfUsxd");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_getwebdata:
                getWebViewData();
                break;
        }
    }

    public class InJavaScript {

    }
}
